package com.example.zhanghao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhanghao.DTO.OrderDto;
import com.example.zhanghao.DTO.UserDto;
import com.example.zhanghao.DTO.WorkDto;
import com.example.zhanghao.DTO.WorkmessageDto;
import com.example.zhanghao.Entity.Orders;
import com.example.zhanghao.Entity.User;
import com.example.zhanghao.Entity.Work;
import com.example.zhanghao.Entity.Workmessage;
import com.example.zhanghao.VO.*;
import com.example.zhanghao.mapper.TypeMapper;
import com.example.zhanghao.mapper.WorkMapper;
import com.example.zhanghao.services.impl.WorkServiceImpl;
import com.example.zhanghao.services.impl.WorkmessageServiceImpl;
import com.example.zhanghao.utils.CopyUtil;
import com.example.zhanghao.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController //这是返回接口
@RequestMapping("/work")
public class WorkController {

    private static final Logger LOG = LoggerFactory.getLogger(WorkController.class);

    @Resource
    private SnowFlake snowFlake;
    @Autowired
    private WorkServiceImpl workServiceImpl;
    @Autowired
    private WorkmessageServiceImpl workmessageServiceImpl;

    @Resource
    WorkMapper workMapper;

    @PostMapping("/add")
    public CommonVo<WorkVo> add(@Valid @RequestBody WorkDto req){
        CommonVo<WorkVo> resp= new CommonVo<>();
        Work work = CopyUtil.copy(req, Work.class);
        workServiceImpl.save(work);
        resp.setData(CopyUtil.copy(req, WorkVo.class));

        Long wordId = work.getId();

        WorkmessageDto workmessageDto = new WorkmessageDto();
        workmessageDto.setWorkId(wordId);
        workmessageDto.setContent(req.getContent());

        workmessageServiceImpl.save(CopyUtil.copy(workmessageDto, Workmessage.class));

        return resp;
    }

    @GetMapping("/lists")
    public CommonVo<PageVo<WorkVo>> lists(@Valid WorkDto req) {
        int offset = (req.getPage()-1)*req.getSize();
        List<WorkVo> workVos = workMapper.selectWorkWithMessage(req.getUserId(),req.getStatus(),req.getSize(), offset);

        LambdaQueryWrapper<Work> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(req.getUserId()!=null,Work::getUserId,req.getUserId());

        long count = workServiceImpl.count(queryWrapper);

        PageVo<WorkVo> pageVo = new PageVo();
        pageVo.setTotal(count);
        pageVo.setPages(req.getPage());
        pageVo.setList(workVos);

        CommonVo<PageVo<WorkVo>> commonVo = new CommonVo<>();
        commonVo.setData(pageVo);

        return commonVo;
    }

    @GetMapping("/getDetail")
    public CommonVo<WorkVo> getDetail(@Valid WorkDto req) {
        WorkVo workVo = workMapper.selectOneWorkWithMessage(req.getId());

        CommonVo<WorkVo> commonVo = new CommonVo<>();
        commonVo.setData(workVo);

        return commonVo;
    }

    @PostMapping("/update")
    public CommonVo<WorkVo> update(@Valid @RequestBody WorkDto req){
        CommonVo<WorkVo> resp= new CommonVo<>();

        workServiceImpl.updateById(CopyUtil.copy(req, Work.class));
        resp.setData(CopyUtil.copy(req, WorkVo.class));

        return resp;
    }

}

